package com.itic.appbase.framework.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;

import net.coobird.thumbnailator.Thumbnails;

/**
 * @author 徐泽威 xuzewei_2012@126.com
 * @time 2017年9月11日 下午1:50:14
 */
public class AdImageUtils {
    private static final int width = 750;
    private static final int height = 1070;
    private static final int hongbaoFontSize = 20;
    private static final int productNameFontSize = 30;
    private static final int originalPriceFontSize = 24;
    private static final int preferentialPriceFontSize = 35;
    private static final int qrCodeCommentFontSize = 20;
    private static final int hongbaoAmountFontSize = 50;
    private static final int productDescFontSize = 24;

    public static void main(String[] args) throws Exception {
        /*Param param = new Param();
        param.setFingerprintImageFilepath("D:/develops/new-workspace/mall_svn/src/main/webapp/static/images/ad/fingerprint.png");
        
        param.setMainImageUrl("http://file.jhjvip.cn/images/hongbao.png");
        param.setHongbao("30");
        param.setPreferentialPrice("115.90");
        param.setOriginalPrice("145.90");
        param.setProductName("另一个佳斯特 专业舒适健身训练高清游泳眼镜。佳斯特 专业舒适健身训练高清游泳眼镜。");
        param.setQrCode("http://blog.csdn.net/u013238535/article/details/53929605?a=1&b=2");
        param.setProductDesc("剁手必买");
        BufferedImage image = gen(param);
        ImageIO.write(image, "jpg", new File("E:\\out.jpg"));*/
    	
    	BufferedImage test = test();
    	ImageIO.write(test, "jpg", new File("E:\\out.jpg"));
    }

    private static List<String> splitProductName(String productName) {
        int firstLineMax = 17;
        List<String> list = new ArrayList<>();
        if (productName.length() <= firstLineMax) {
            // 单行
            list.add(productName);
            return list;
        }
        // 两行
        list.add(productName.substring(0, firstLineMax));
        String remaining = productName.substring(firstLineMax);
        int nextLineMax = 14;
        if (remaining.length() > nextLineMax) {
            list.add(remaining.substring(0, nextLineMax) + "...");
        } else {
            list.add(remaining.substring(0, remaining.length()));
        }
        return list;
    }
    
    public static BufferedImage test()throws Exception {
    	 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);       
         Graphics2D graphics = (Graphics2D)image.getGraphics();
         graphics.setColor(Color.WHITE);
         graphics.fillRect(0, 0, width, height);
         
         BufferedImage mainImageThumbnail = Thumbnails.of(new URL("http://file.jhjvip.cn/images/216bcfb4-2846-4efb-9f3e-0ab5d26dd704.jpg")).size(width, width).asBufferedImage();
         graphics.drawImage(mainImageThumbnail, 0, 0, null);
         return image;
    }

    public static BufferedImage gen(Param param) throws Exception {
    	
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);       
        Graphics2D graphics = (Graphics2D)image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);

        BufferedImage mainImageThumbnail = Thumbnails.of(new URL(param.getMainImageUrl())).size(width, width).asBufferedImage();
        graphics.drawImage(mainImageThumbnail, 0, 0, null);

        if( StringHelper.isNotBlank(param.getHongbao()) && NumberStringHelper.isGreaterNoEqual(param.getHongbao(), "0") ){
            graphics.setFont(new Font("微软", Font.PLAIN, productNameFontSize));
            graphics.setColor(new Color(0x323232));
            List<String> splitProductName = splitProductName(param.getProductName());
            graphics.drawString(splitProductName.get(0), 38, width + 32 + productNameFontSize);
            for (int i = 1; i < splitProductName.size(); i++) {
                graphics.drawString(splitProductName.get(i), 38, width + 32 + (productNameFontSize + 5) * (i + 1));
            }
        }else{
        	graphics.setFont(new Font("微软", Font.PLAIN, productNameFontSize));
            graphics.setColor(new Color(0x323232));
            List<String> splitProductName = splitProductName(param.getProductName());
            graphics.drawString(splitProductName.get(0), 38, width + 32 + productNameFontSize);
            for (int i = 1; i < splitProductName.size(); i++) {
                graphics.drawString(splitProductName.get(i), 38, width + 32 + (productNameFontSize + 5) * (i + 1));
            }
        }

        graphics.setFont(new Font("微软", Font.PLAIN, preferentialPriceFontSize));
        graphics.setColor(new Color(0xff3100));
        graphics.drawString(String.format("劵后价：￥%s", param.getPreferentialPrice()), 38, width + 32 + 36 + 50 + 40 );

        graphics.setFont(new Font("微软", Font.PLAIN, originalPriceFontSize));
        graphics.setColor(new Color(0x9a9a9a));
        graphics.drawString(String.format("友惠价：￥%s", param.getOriginalPrice()), 38, width + 32 + 36 + 50 + 50 + 28);
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BufferedImage qrCodeBufferedImage = MatrixToImageWriter.toBufferedImage(new MultiFormatWriter().encode(param.getQrCode(), BarcodeFormat.QR_CODE, 250, 250, hints));
        graphics.drawImage(qrCodeBufferedImage, 38 + 440, width, null);

        String advice="";
        if( StringHelper.isNotBlank(param.getProductDesc())){
        	advice = "推荐理由："+param.getProductDesc();
        }else{
        	advice = "推荐理由：疯狂促销已经开始，历史最低价，手慢无！";
        }
        
        graphics.setFont(new Font("微软", Font.PLAIN, productDescFontSize));
        graphics.setColor(new Color(0x323232));
        List<String> splitProductDesc = splitProductName(advice);
        graphics.drawString(splitProductDesc.get(0), 38, width + 210 + productNameFontSize);
        for (int i = 1; i < splitProductDesc.size(); i++) {
            graphics.drawString(splitProductDesc.get(i), 38, width + 210 + (productNameFontSize + 5) * (i + 1));
        }
        
        graphics.setFont(new Font("微软", Font.PLAIN, qrCodeCommentFontSize));
        graphics.setColor(new Color(0x323232));
        graphics.drawString("长按二维码", 38 + 480, width + 32 + 36 + 50 + 56 + 50 + 28 + 5);
        graphics.drawString("购买商品", 38 + 480, width + 32 + 36 + 50 + 56 + 50 + 28 + 30 + 5);

        graphics.drawImage(ImageIO.read(new File(param.getFingerprintImageFilepath())), 38 + 440 + 160, width + 32 + 36 + 50 + 56 + 55 + 8, null);

        graphics.drawImage(ImageIO.read(new File(param.getHongbaoImageFilepath())), width - 190, width - 200, null);

        graphics.setFont(new Font("微软", Font.PLAIN, hongbaoAmountFontSize));
        graphics.setColor(Color.WHITE);
        graphics.drawString(param.getHongbao(), width - 155, width - 90);

        graphics.dispose();
      
        return image;
    }

    public static BufferedImage genJd(Param param) throws Exception {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = (Graphics2D)image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);

        BufferedImage mainImageThumbnail = Thumbnails.of(new URL(param.getMainImageUrl())).size(width, width).asBufferedImage();
        graphics.drawImage(mainImageThumbnail, 0, 0, null);

        graphics.setColor(Color.RED);
        graphics.fillRoundRect(38, width + 32, 140, 36, 8, 8);

        if( StringHelper.isNotBlank(param.getHongbao()) && NumberStringHelper.isGreaterNoEqual(param.getHongbao(), "0")){
        	graphics.setColor(Color.RED);
            graphics.fillRoundRect(38, width + 32, 140, 36, 8, 8);
        	graphics.setFont(new Font("微软", Font.PLAIN, hongbaoFontSize));
            graphics.setColor(Color.WHITE);
            graphics.drawString(String.format("红包%s元", param.getHongbao()), 38 + 18, width + 32 + hongbaoFontSize);
            
            graphics.setFont(new Font("微软", Font.PLAIN, productNameFontSize));
            graphics.setColor(new Color(0x323232));
            List<String> splitProductName = splitProductName(param.getProductName());
            graphics.drawString(splitProductName.get(0), 38 + 140 + 20, width + 32 + productNameFontSize);
            for (int i = 1; i < splitProductName.size(); i++) {
                graphics.drawString(splitProductName.get(i), 38, width + 32 + (productNameFontSize + 5) * (i + 1));
            }
        }else{
        	graphics.setFont(new Font("微软", Font.PLAIN, productNameFontSize));
            graphics.setColor(new Color(0x323232));
            List<String> splitProductName = splitProductName(param.getProductName());
            graphics.drawString(splitProductName.get(0), 38, width + 32 + productNameFontSize);
            for (int i = 1; i < splitProductName.size(); i++) {
                graphics.drawString(splitProductName.get(i), 38, width + 32 + (productNameFontSize + 5) * (i + 1));
            }
        }

        graphics.setFont(new Font("微软", Font.PLAIN, preferentialPriceFontSize));
        graphics.setColor(new Color(0xff3100));
        graphics.drawString(String.format("劵后价：￥%s", param.getPreferentialPrice()), 38, width + 32 + 36 + 50 + 40 );

        graphics.setFont(new Font("微软", Font.PLAIN, originalPriceFontSize));
        graphics.setColor(new Color(0x9a9a9a));
        graphics.drawString(String.format("京东价：￥%s", param.getOriginalPrice()), 38, width + 32 + 36 + 50 + 50 + 28);
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BufferedImage qrCodeBufferedImage = MatrixToImageWriter.toBufferedImage(new MultiFormatWriter().encode(param.getQrCode(), BarcodeFormat.QR_CODE, 250, 250, hints));
        graphics.drawImage(qrCodeBufferedImage, 38 + 440, width, null);

        String advice="";
        if( StringHelper.isNotBlank(param.getProductDesc())){
        	advice = "推荐理由："+param.getProductDesc();
        }else{
        	advice = "推荐理由：疯狂促销已经开始，历史最低价，手慢无！";
        }
        graphics.setFont(new Font("微软", Font.PLAIN, productDescFontSize));
        graphics.setColor(new Color(0x323232));
        List<String> splitProductDesc = splitProductName(advice);
        graphics.drawString(splitProductDesc.get(0), 38, width + 210 + productNameFontSize);
        for (int i = 1; i < splitProductDesc.size(); i++) {
            graphics.drawString(splitProductDesc.get(i), 38, width + 210 + (productNameFontSize + 5) * (i + 1));
        }
        
        
        graphics.setFont(new Font("微软", Font.PLAIN, qrCodeCommentFontSize));
        graphics.setColor(new Color(0x323232));
        graphics.drawString("长按二维码", 38 + 480, width + 32 + 36 + 50 + 56 + 50 + 28 + 5);
        graphics.drawString("购买商品", 38 + 480, width + 32 + 36 + 50 + 56 + 50 + 28 + 30 + 5);

        graphics.drawImage(ImageIO.read(new File(param.getFingerprintImageFilepath())), 38 + 440 + 160, width + 32 + 36 + 50 + 56 + 55 + 8, null);

        graphics.drawImage(ImageIO.read(new File(param.getHongbaoImageFilepath())), width - 190, width - 200, null);

        graphics.setFont(new Font("微软", Font.PLAIN, hongbaoAmountFontSize));
        graphics.setColor(Color.WHITE);
        graphics.drawString(param.getHongbao(), width - 155, width - 90);

        graphics.dispose();
        
        return image;
    }
    
    public static class Param {
        private String mainImageUrl;//主图URL
        private String productName;//商品名称
        private String originalPrice;//商品原价
        private String preferentialPrice;//优惠价
        private String hongbao;//红包价格
        private int salesVolume;//销量
        private String qrCode;//二维码对应 地址
        private String hongbaoImageFilepath;//主图上添加的一层红包路径
        private String fingerprintImageFilepath;//手指图片路径
        private String productDesc;

        public String getMainImageUrl() {
            return mainImageUrl;
        }

        public void setMainImageUrl(String mainImageUrl) {
            this.mainImageUrl = mainImageUrl;
        }

        public String getProductName() {
            return productName;
        }

        public void setProductName(String productName) {
            this.productName = productName;
        }

        public String getOriginalPrice() {
            return originalPrice;
        }

        public void setOriginalPrice(String originalPrice) {
            this.originalPrice = originalPrice;
        }

        public String getPreferentialPrice() {
            return preferentialPrice;
        }

        public void setPreferentialPrice(String preferentialPrice) {
            this.preferentialPrice = preferentialPrice;
        }

        public String getHongbao() {
            return hongbao;
        }

        public void setHongbao(String hongbao) {
            this.hongbao = hongbao;
        }

        public int getSalesVolume() {
            return salesVolume;
        }

        public void setSalesVolume(int salesVolume) {
            this.salesVolume = salesVolume;
        }

        public String getQrCode() {
            return qrCode;
        }

        public void setQrCode(String qrCode) {
            this.qrCode = qrCode;
        }

        public String getHongbaoImageFilepath() {
            return hongbaoImageFilepath;
        }

        public void setHongbaoImageFilepath(String hongbaoImageFilepath) {
            this.hongbaoImageFilepath = hongbaoImageFilepath;
        }

        public String getFingerprintImageFilepath() {
            return fingerprintImageFilepath;
        }

        public void setFingerprintImageFilepath(String fingerprintImageFilepath) {
            this.fingerprintImageFilepath = fingerprintImageFilepath;
        }

		public String getProductDesc() {
			return productDesc;
		}

		public void setProductDesc(String productDesc) {
			this.productDesc = productDesc;
		}
        
        
    }
}
